home *** CD-ROM | disk | FTP | other *** search
- /*
- File: FWOrdCol.cpp
-
- Contains: Implementation of FW_CPrivOrderedCollection and FW_COrderedCollectionIterator
-
- Written by: Richard Rodseth
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <4> 6/20/94 RR ODMemoryHeap* -> ODMemoryHeapID
- <3> 6/15/94 NP ODHeap change.
- <2> 6/9/94 RR Remove ASLM Stuff.
- <3> 3/15/94 MB Changes to support SCpp/ASLM builds,
- #1150864.
- <2> 2/15/94 CG #1143680 - OrdColls now allocated on
- specified heap.
- <8> 2/7/94 JA Tiger Team Makeover!
- <7> 2/2/94 NP Added support for allocating internal
- structures in given heap.
- <6> 1/19/94 NP Fixed Remove to delete the link.
- <5> 11/19/93 NP Moved definition of FW_CPrivValueLink to FWOrdCol.h
- <4> 10/28/93 RR Removed ambiguous constructor
- <3> 8/13/93 CG Added casting required by CFront.
- <1> 8/13/93 RCR first checked in
-
- To Do:
- */
-
- #include "FWFound.hpp"
-
- #ifndef FWORDCOL_H
- #include "FWOrdCol.h"
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwcollec
- #endif
-
- //======================================================================================
- // Class FW_CPrivValueLink - Implementation
- //======================================================================================
-
- FW_CPrivValueLink::FW_CPrivValueLink(FW_ElementType value)
- {
- fValue = value;
- }
-
- FW_CPrivValueLink::~FW_CPrivValueLink()
- {
- }
-
- //======================================================================================
- // Class FW_CPrivOrderedCollection
- //======================================================================================
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::FW_CPrivOrderedCollection
- //------------------------------------------------------------------------------
-
- FW_CPrivOrderedCollection::FW_CPrivOrderedCollection()
- {
- fHeap = NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::FW_CPrivOrderedCollection
- //------------------------------------------------------------------------------
-
- FW_CPrivOrderedCollection::FW_CPrivOrderedCollection(void* where)
- {
- fHeap = where;
- }
-
- // FW_CPrivOrderedCollection::~FW_CPrivOrderedCollection
- //------------------------------------------------------------------------------
-
- FW_CPrivOrderedCollection::~FW_CPrivOrderedCollection()
- {
- this->RemoveAll();
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::AddFirst
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::AddFirst(FW_ElementType element)
- {
- FW_CPrivValueLink* newLink = this->CreateNewLink(element);
- fImplementation.AddFirst(newLink);
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::AddLast
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::AddLast(FW_ElementType element)
- {
- FW_CPrivValueLink* newLink = CreateNewLink(element);
- fImplementation.AddLast(newLink);
- }
-
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::AddBefore
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::AddBefore(FW_ElementType existing, FW_ElementType tobeadded)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
- while (aLink != NULL)
- {
- FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- FW_CPrivValueLink* newLink = CreateNewLink(tobeadded);
- fImplementation.AddBefore(*aLink, newLink);
- aLink = NULL;
- }
- else
- aLink = (FW_CPrivValueLink*) iter.Next();
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::AddAfter
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::AddAfter(FW_ElementType existing, FW_ElementType tobeadded)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
- while (aLink != NULL)
- {
- FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- FW_CPrivValueLink* newLink = CreateNewLink(tobeadded);
- fImplementation.AddAfter(*aLink, newLink);
- aLink = NULL;
- }
- else
- aLink = (FW_CPrivValueLink*) iter.Next();
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::After
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::After(FW_ElementType existing)
- {
- FW_CPrivValueLink* linkAfter = NULL;
-
- FW_CPrivLinkedListIterator iter(&fImplementation);
-
- for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
- {
- FW_ElementType v = ((FW_CPrivValueLink*) link)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- linkAfter = (FW_CPrivValueLink*) fImplementation.After(*link);
- break;
- }
- }
-
- return linkAfter ? linkAfter->GetValue() : (FW_ElementType)NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::Before
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::Before(FW_ElementType existing)
- {
- FW_CPrivValueLink* linkBefore = NULL;
-
- FW_CPrivLinkedListIterator iter(&fImplementation);
-
- for (FW_CPrivValueLink* link = (FW_CPrivValueLink*) iter.First(); iter.IsNotComplete(); link = (FW_CPrivValueLink*) iter.Next())
- {
- FW_ElementType v = ((FW_CPrivValueLink*) link)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- linkBefore = (FW_CPrivValueLink*) fImplementation.Before(*link);
- break;
- }
- }
-
- return linkBefore ? linkBefore->GetValue() : (FW_ElementType)NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::First
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::First()
- {
- FW_CPrivValueLink* firstLink = (FW_CPrivValueLink*) fImplementation.First();
- return firstLink ? firstLink->GetValue() : (FW_ElementType)NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::Last
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::Last()
- {
- FW_CPrivValueLink* lastLink = (FW_CPrivValueLink*) fImplementation.Last();
- return lastLink ? lastLink->GetValue() : (FW_ElementType)NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::RemoveFirst
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::RemoveFirst()
- {
- FW_ElementType value = NULL;
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) fImplementation.RemoveFirst();
- if (aLink != NULL)
- {
- value = aLink->GetValue();
- delete aLink;
- }
- return value;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::RemoveLast
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_CPrivOrderedCollection::RemoveLast()
- {
- FW_ElementType value = NULL;
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) fImplementation.RemoveLast();
- if (aLink != NULL)
- {
- value = aLink->GetValue();
- delete aLink;
- }
- return value;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::Remove
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::Remove(FW_ElementType existing)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
- while (aLink != NULL)
- {
- FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- fImplementation.Remove(*aLink);
- delete aLink;
- aLink = NULL;
- }
- else
- aLink = (FW_CPrivValueLink*) iter.Next();
- }
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::RemoveAll
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::RemoveAll()
- {
- FW_CPrivLink* link = fImplementation.RemoveFirst();
- while (link != NULL)
- {
- delete link;
- link = fImplementation.RemoveFirst();
- }
- }
-
- /*
- // this is too dangerous because the destructor of value will never be called
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::DeleteAll
- //------------------------------------------------------------------------------
-
- void FW_CPrivOrderedCollection::DeleteAll()
- {
- FW_CPrivLink* link = fImplementation.RemoveFirst();
- while (link != NULL)
- {
- FW_ElementType value = ((FW_CPrivValueLink*) link)->GetValue();
- delete value;
- delete link;
- link = fImplementation.RemoveFirst();
- }
- }
- */
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::Contains
- //------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivOrderedCollection::Contains(FW_ElementType existing)
- {
- FW_CPrivLinkedListIterator iter(&fImplementation);
- FW_CPrivValueLink* aLink = (FW_CPrivValueLink*) iter.First();
- while (aLink != NULL)
- {
- FW_ElementType v = ((FW_CPrivValueLink*) aLink)->GetValue();
-
- if (this->ElementsMatch(v,existing))
- {
- return TRUE;
- }
- else
- aLink = (FW_CPrivValueLink*) iter.Next();
- }
- return FALSE;
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::CreateIterator
- //------------------------------------------------------------------------------
-
- FW_COrderedCollectionIterator* FW_CPrivOrderedCollection::CreateIterator()
- {
- // return new(fHeap) FW_COrderedCollectionIterator(this);
- return new FW_COrderedCollectionIterator(this);
- }
-
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::CreateNewLink
- //------------------------------------------------------------------------------
-
- FW_CPrivValueLink* FW_CPrivOrderedCollection::CreateNewLink(FW_ElementType value) const
- {
- // return new (fHeap) FW_CPrivValueLink(value);
- return new FW_CPrivValueLink(value);
- }
-
- //------------------------------------------------------------------------------
- // FW_CPrivOrderedCollection::ElementsMatch
- //------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivOrderedCollection::ElementsMatch(FW_ElementType v1,FW_ElementType v2) const
- {
- return (v1 == v2);
- }
-
- //======================================================================================
- // FW_COrderedCollectionIterator
- //======================================================================================
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::FW_COrderedCollectionIterator
- //------------------------------------------------------------------------------
-
- FW_COrderedCollectionIterator::FW_COrderedCollectionIterator(FW_CPrivOrderedCollection* collection)
- : fImplementation(collection ? &(collection->fImplementation) : (FW_CPrivLinkedList*)NULL)
- {
- fCollection = collection;
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::~FW_COrderedCollectionIterator
- //------------------------------------------------------------------------------
-
- FW_COrderedCollectionIterator::~FW_COrderedCollectionIterator()
- {
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::First
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_COrderedCollectionIterator::First()
- {
- if (fCollection)
- {
- FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.First();
-
- return link ? link->GetValue() : (FW_ElementType)NULL;
- }
- else
- return NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::Next
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_COrderedCollectionIterator::Next()
- {
- if (fCollection)
- {
- FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Next();
-
- return link ? link->GetValue() : (FW_ElementType)NULL;
- }
- else
- return NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::Last
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_COrderedCollectionIterator::Last()
- {
- if (fCollection)
- {
- FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Last();
-
- return link ? link->GetValue() : (FW_ElementType)NULL;
- }
- else
- return NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::Previous
- //------------------------------------------------------------------------------
-
- FW_ElementType FW_COrderedCollectionIterator::Previous()
- {
- if (fCollection)
- {
- FW_CPrivValueLink* link = (FW_CPrivValueLink*) fImplementation.Previous();
-
- return link ? link->GetValue() : (FW_ElementType)NULL;
- }
- else
- return NULL;
- }
-
- //------------------------------------------------------------------------------
- // FW_COrderedCollectionIterator::IsNotComplete
- //------------------------------------------------------------------------------
-
- FW_Boolean FW_COrderedCollectionIterator::IsNotComplete()
- {
- return fCollection ? fImplementation.IsNotComplete() : FALSE;
- }
-
-
-
-
-
-
-
-